﻿using System;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 资源加载来源
/// </summary>
public enum LoadSource
{
    /// <summary>
    /// 优先从应用数据目录加载，找不到再从安装包加载
    /// </summary>
    Auto,
    /// <summary>
    /// 从安装包加载
    /// </summary>
    Package,
    /// <summary>
    /// 从应用数据目录加载
    /// </summary>
    Data,
}

/// <summary>
/// 加载任务基类
/// 支持yield return
/// </summary>
public abstract class LoadTask
{
    /// <summary>
    /// 资源对象
    /// </summary>
    public object resource;

    /// <summary>
    /// 资源类型
    /// </summary>
    public Type resourceType;

    /// <summary>
    /// 资源路径
    /// </summary>
    public string path;

    /// <summary>
    /// 资源来源
    /// </summary>
    public LoadSource source;

    /// <summary>
    /// 加载进度
    /// </summary>
    public abstract float progress
    { get; }

    /// <summary>
    /// 加载错误信息
    /// </summary>
    public abstract string error
    { get; }

    /// <summary>
    /// 是否加载完成，注意:只是加载完成，不代表加载成功，需要根据error是否为空判断是否加载成功
    /// </summary>
    public abstract bool isDone
    { get; }

    /// <summary>
    /// 更新入口
    /// </summary>
    public virtual void Update()
    { }
    
    // 加载完成
    protected virtual void OnFinish()
    {
    }

    /// <summary>
    /// 销毁对象
    /// </summary>
    public virtual void Destroy()
    {
        resource = null;
    }

    /// <summary>
    /// 判断路径是否相同
    /// </summary>
    public bool PathEquals(string path, LoadSource source)
    {
        if (this.source != source)
        {
            return false;
        }

        if (!LoadManager.PathEquals(this.path, path))
        {
            return false;
        }

        return true;
    }
}

/// <summary>
/// 加载返回对象
/// </summary>
public class LoadTaskR : IEnumerator
{
    static int NextTaskID = 0;
    static LoadTaskR m_Current = new LoadTaskR(null, false);
    public static void SetCurrent(LoadTask task)
    {
        m_Current.task = task;
    }
    public static LoadTaskR current
    {
        get { return new LoadTaskR(m_Current.task, false); }
    }

    public LoadTask task;

    /// <summary>
    /// 资源路径
    /// </summary>
    public string path
    { get { return task.path; } }

    /// <summary>
    /// 资源来源
    /// </summary>
    public LoadSource source
    { get { return task.source; } }

    /// <summary>
    /// 加载进度
    /// </summary>
    public float progress
    { get { return task.progress; } }

    /// <summary>
    /// 加载错误信息
    /// </summary>
    public string error
    { get { return task.error; } }

    /// <summary>
    /// 是否加载完成，注意:只是加载完成，不代表加载成功，需要根据error是否为空判断是否加载成功
    /// </summary>
    public bool isDone
    { get { return task.isDone; } }

    public object resource
    { get { return task.resource; } }

    public int taskID;

    public LoadTaskR(LoadTask task, bool retain = true)
    {
        this.task = task;
        taskID = NextTaskID++;
    }

   

    /// <summary>
    /// 卸载任务,释放对资源的引用
    /// </summary>
    public void Unload()
    {
        if (task == null)
            return;

        task = null;
    }
    
    public object Current
    { get { return null; } }

    public virtual bool MoveNext()
    {
        return !isDone;
    }

    public void Reset()
    { }
}
